﻿using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneStateController
{
    public bool isRunStart;
    private AsyncOperation mAo;
    private ISceneState mState;

    // 设置状态
    public void SetState(ISceneState state, bool isLoadScene = true)
    {
        if (mState != null) mState.StateEnd();
        mState = state;
        if (isLoadScene)
        {
            Debug.Log("llmState.SceneName");
            Debug.Log(mState.SceneName);
            mAo = SceneManager.LoadSceneAsync(mState.SceneName);
            isRunStart = false;
        }
        else
        {
            Debug.Log("oomState.SceneName");
            Debug.Log(mState.SceneName);
            mState.StateStart();
            isRunStart = true;
        }
    }

    // 更新状态
    public void StateUpdate()
    {
        if (mAo != null && !mAo.isDone)
        {
            // 加载中,还未初始化
            Debug.Log("加载中");

            return;
        }

        // 进行初始化
        if (isRunStart == false && mAo != null && mAo.isDone)
        {
            Debug.Log("加载完成,开始初始化");
            // mAo.progress == 1 或 mAo.isDone == true 都无法获取到新场景中的游戏物体
            mState.StateStart();
            isRunStart = true;
        }

        // if (mAo == null) return;


        // Debug.Log(mAo.isDone);

        // 更新
        if (mState != null && isRunStart)
            // Debug.Log("开始更新");
            mState.StateUpdate();
    }
}